package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    @ApiOperation(value = "新增菜品")
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("菜品信息:{}", dishDTO);
        dishService.save(dishDTO);

        //清理缓存
        String key = "dish_" + dishDTO.getCategoryId();
        CleanCache(key);
        return Result.success();
    }


    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {

        log.info("菜品分页查询,页数:{},大小:{}", dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        //调用查询方法返回总记录数 和当前页数
        PageResult pageResult = dishService.PageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }


    @PostMapping(value = "/status/{status}")
    @ApiOperation("更新菜品状态")
    public Result statusOrPost(@PathVariable Integer status, Long id) {
        log.info("菜品跟新数据,{} ,{}" + status, id);

        dishService.UpdateStatus(status, id);
        CleanCache("dish_*");
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result DeleteDish(@RequestParam List<Long> ids) {
        log.info("删除菜品信息:{}" + ids);
        dishService.Delete(ids);
        CleanCache("dish_*");
        return Result.success();
    }

    @GetMapping(value = "/{id}")
    @ApiOperation("根据ID查询菜品")
    public Result<DishVO> GetDishById(@PathVariable Long id) {
        log.info("菜品id:{}" + id);

        DishVO dishVO = dishService.GetDishById(id);

        return Result.success(dishVO);
    }

    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("菜品信息:{}" + dishDTO);

        dishService.Update(dishDTO);

        CleanCache("dish_*");

        return Result.success();

    }

    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> GetCategoryId(Long categoryId) {
        log.info("分类id:{}" + categoryId);
        List<Dish> dish = dishService.getCategoryId(categoryId);

        return Result.success(dish);
    }


    /**
     * 清理缓存
     * dish_* 所有key获取
     *
     * @param key
     */
    private void CleanCache(String key) {
        Set keys = redisTemplate.keys(key);
        redisTemplate.delete(keys);
    }
}
